package com.softer.chengxin.dicinfo.controller;

import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.dicinfo.entity.DicType;
import com.softer.chengxin.dicinfo.service.DicTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author zcz
 * @date 2018/12/20
 */
@RestController
@RequestMapping("/api/dicType")
@Slf4j
public class DicTypeController {
    @Autowired
    private DicTypeService dicTypeService;
    @PostMapping("/getData")
    public ManagerPageResult getData(Page page,DicType dicType) {
        return dicTypeService.getData(page,dicType);
    }

    /**
     * 新增
     * @param dicType
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(DicType dicType,HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        dicType.setAdder(loginName);
        return dicTypeService.add(dicType);
    }

    /**
     * 修改
     * @param dicType
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(DicType dicType,HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        dicType.setAdder(loginName);
        return dicTypeService.update(dicType);
    }
    @PostMapping("/getDicTypeById")
    public DicType getDicTypeById(String id) {
        return dicTypeService.getDicTypeById(id);
    }

}
